home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 7055 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.4 KB  |  70 lines

  1. Path: ix.netcom.com!netnews
  2. From: David Brownell <brownell@ix.netcom.com>
  3. Newsgroups: comp.programming.threads,comp.lang.c++,comp.unix.osf.osf1,comp.unix.programmer,comp.object
  4. Subject: Re: Looking for best design for using pthreads in C++ objects
  5. Date: Tue, 20 Feb 1996 10:09:35 -0800
  6. Organization: Dave's VAX
  7. Message-ID: <312A0E5F.7B2C@ix.netcom.com>
  8. References: <3128ff8b.666031216@news.clark.net>
  9. NNTP-Posting-Host: pax-ca8-25.ix.netcom.com
  10. Mime-Version: 1.0
  11. Content-Type: text/plain; charset=us-ascii
  12. Content-Transfer-Encoding: 7bit
  13. X-NETCOM-Date: Tue Feb 20 10:13:30 AM PST 1996
  14. X-Mailer: Mozilla 2.0 (Win95; I)
  15.  
  16. Thom Anderson wrote:
  17.  
  18. > I have used pthreads from C before but not from C++.  I have a couple of ideas
  19. > in mind but neither is elegant.  Does anyone have any experience with
  20. > C++/Pthreads and no of some good design solutions, tips, or ideas???
  21.  
  22. One approach that works just fine:  use it from C++ like you use it from C.
  23.  
  24. If your C++ environment has good pthreads supports, you'll be able to forget
  25. about pushing/popping cleanup handlers since the C++ destructors will do all
  26. of that stuff for you when your thread is canceled.  (Cancel != Kill ...)
  27. You _really_ want that support, else you won't be able to support cancellation
  28. without lots of additional effort.
  29.  
  30. One of the really nice techniques is to have a "Locker" class to grab mutexes
  31. as needed, and then release it automatically on all exits.  Something like:
  32.  
  33.     class Locker {
  34.       public:
  35.                 Locker (pthread_mutex_t *lock)
  36.                 : mutex (lock) { pthread_mutex_lock (mutex); }
  37.                 ~Locker () { pthread_mutex_unlock (mutex); }
  38.       
  39.         int            wait (pthread_cond_t *cv)
  40.                 { return pthread_cond_wait (cv, mutex); };
  41.     
  42.       private:
  43.         pthread_mutex_t    *mutex;
  44.     };
  45.  
  46. and then use it like
  47.  
  48.     SomeClass::member_function ( ... arguments ...)
  49.     {
  50.         ... do a bunch of work
  51.  
  52.         Locker (&some_mutex);
  53.  
  54.         ... do more work
  55.         ... The mutex is held during this work, and always
  56.         ... released on all exits from this function
  57.     }
  58.  
  59. That kind of class really helps get rid of the bugs you have due
  60. to locks not getting released uniformly on all codepaths.
  61.  
  62. I've never found a "thread" class to be of any help except as an
  63. excercise to show basic C++ and POSIX.1c skills.  CFRONT used to
  64. have a thread class used for simulations, and there are folk who
  65. evidently like such classes.  Having programmed MT/C++ software
  66. for the past four years or so, I can testify that they're not
  67. necessary.  But of course, your preferred style can differ.
  68.  
  69. - Dave
  70.